package CtrLayer;
import ModLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class SaleCtr here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleCtr
{
    private SaleCon salecon;
    private PersonCtr personctr;
    private ProductCtr productctr;
    private int location;
    private TimeAndCheck time;
    public static SaleCtr instance;

    private SaleCtr()
    {
        salecon = salecon.getInstance();
        personctr = personctr.getInstance();
        time = time.getInstance();
        productctr = productctr.getInstance();
    }

    public static SaleCtr getInstance()
    {
        if(instance==null)
            instance = new SaleCtr();
        return instance;
    }

    public void setLocation(String location)
    {
        if(location == "DIY")
            this.location = 1;
        else if(location == "Bathroom")
            this.location = 2;
        else
            this.location = 3;
    }

    public ArrayList<Sale> searchSale()
    {
        return salecon.getSaleList();
    }

    public boolean createSale(String id, String IDEmployee, String IDCustomer)
    {
        if(personctr.searchPerson(IDEmployee, "office") instanceof Employee)
        {
            String creationDate = time.getActualDate();
            salecon.createSale(id, IDEmployee, IDCustomer, creationDate);
            return true;
        }
        return false;
    }

    public int deleteOutDatedWishlist()
    {
        int counterOfDeleted = 0;
        String deleteObject = null;
        boolean leapYearOK = false;
        String date = time.getActualDate();
        int[] daysInMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int month = Integer.parseInt(date.substring(3,5));
        int day = Integer.parseInt(date.substring(0,2));
        int year = Integer.parseInt(date.substring(6,10));
        if(salecon.getSaleList().size()!=0)
        {
            if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
                leapYearOK = true;
            if((day - 7) > 0)
            {
                if(leapYearOK == true)
                    deleteObject = changeIntToString((day - 6), month, year);
                else
                    deleteObject = changeIntToString((day - 7), month, year);
            }
            else
            {
                int dayLocal = 0;
                if(month == 1)
                {
                    dayLocal = ((daysInMonth[12]) + (day - 7));
                }
                else
                {
                    dayLocal = ((daysInMonth[month - 1]) + (day - 7));
                }
                if(month == 
                1 && ((day - 7) < 1))
                {
                    if(leapYearOK == true)
                        deleteObject = changeIntToString((dayLocal + 1), (12), (year - 1));
                    else
                        deleteObject = changeIntToString(dayLocal, (12), (year - 1));
                }
                else
                {
                    if(leapYearOK == true)
                        deleteObject = changeIntToString((dayLocal + 1), (month - 1), year);
                    else
                        deleteObject = changeIntToString(dayLocal, (month - 1), year);
                }
            }
            counterOfDeleted = salecon.deleteOutDatedWishlist(deleteObject);
        }
        return counterOfDeleted;
    }

    private String changeIntToString(int day, int month, int year)
    {
        if(day < 10 && month < 10)
            return "0"+day+ "-0" +month+ "-" +year;
        else if(day < 10 && month >= 10)
            return "0"+day+ "-"+month+ "-" +year;
        else if(day > 9 && month >= 10)
            return day+ "-" +month+ "-" +year;
        else if(day >= 10 && month < 10)
            return day+ "-0" +month+ "-" +year;
        else 
            return null;
    }

    public boolean isCustomerRegistered(Sale s)
    {
        PersonInformations cobj = personctr.searchPerson(s.getIDCustomer(), "employee");
        Customer c = (Customer) cobj;
        if(c == null)
        {
            return false;
        }
        return true;
    }

    public double getSum(Sale sale, double discount)
    {
        double sum = 0;
        ArrayList<String> productList = sale.getProductList();
        ArrayList<Integer> quaList = sale.getQuaList();
        if(productList != null)
        {
            for(int i = 0; i <= productList.size()-1; i++)
            {
                int q = quaList.get(i);
                Product p = productctr.findProduct(productList.get(i));
                if(q<=p.getPallet()&&p.getPallet()!=1)
                {
                    sum += p.getPalletDiscount()/100*(100-discount);
                    q -= p.getPallet();
                }
                else
                {
                    if(productctr.findProduct(productList.get(i)).getDiscount() == 0)
                        sum += p.getPrice(discount)*q;
                    else
                        sum += p.getPrice(p.getDiscount())*q;
                }
            }
        }
        return sum;
    }

    public void removeSale(String id)
    {
        salecon.removeSale( id);
    }

    public Sale findSale(String id)
    {
        return salecon.findSale(id);
    }

    public void removeProduct(String id, String idProduct)
    {
        salecon.removeProduct(id, idProduct);
    }

    public void addProduct(String id, String idProduct, int qua)
    {
        salecon.addProduct(id, idProduct, qua);
    }

    public void checkout(String id, double discount)
    {
        Sale s = salecon.findSale(id);
        double sum = getSum(s, Double.parseDouble(personctr.getInfoCompany("6")));
        sum = sum/100*(100-discount);
        PersonInformations cobj = personctr.searchPerson(s.getIDCustomer(), "office");
        PersonInformations eobj = personctr.searchPerson(s.getIDEmployee(), "office");
        Customer c;
        if(cobj instanceof Customer)
        {
            c = (Customer) cobj;
        }
        else
        c = null;
        Employee e = (Employee) eobj;
        if(c==null)
        {
            //placeholder for a method that would deduct the sum from an unregistered customer's account
        }
        else
        {
            double total = Double.parseDouble(c.getOwe())+sum;
            c.setOwe(String.valueOf(total));
        }
        String em = s.getIDEmployee();
        String read = personctr.getInfoEmployee(em, "8");
        double total = Double.parseDouble(read)+sum;
        personctr.updateEmployee(s.getIDEmployee(), String.valueOf(total), "8");

        ArrayList<String> pList = s.getProductList();
        ArrayList<Integer> qList = s.getQuaList();

        for(int i=0; i<=pList.size()-1; i++)
        {
            if(location == 1)
                productctr.findProduct(pList.get(i)).setStockDIY(productctr.findProduct(pList.get(i)).getStockDIY()-qList.get(i));
            else if(location == 2)
                productctr.findProduct(pList.get(i)).setStockBath(productctr.findProduct(pList.get(i)).getStockBath()-qList.get(i));
            else
                productctr.findProduct(pList.get(i)).setStockKit(productctr.findProduct(pList.get(i)).getStockKit()-qList.get(i));
        }
    }
}